#include "logindialog.h"
#include "ui_logindialog.h"
#include<QPushButton>
#include<memory>
#include<QLineEdit>
#include<QJsonObject>
#include"httpmgr.h"
#include<toast.h>
LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    setupconnection();

}
void LoginDialog::setupconnection(){
    connect(_HttpMgr::GetInstance().get(),&HttpMgr::sig_login_http_finish,this,&LoginDialog::slot_login_http_finish);
    connect(ui->reg_btn,&QPushButton::clicked,this,&LoginDialog::SwitchToReg);
    connect(ui->forget_label,&ClickableLabel::clicked,[this](){
        emit SwitchToResetpsw();
    });
    connect(ui->user_edit,&QLineEdit::editingFinished,this,&LoginDialog::onUsertextChange);
    connect(ui->psw_edit,&QLineEdit::editingFinished,this,&LoginDialog::onPswtextChange);
}
void LoginDialog::InitHttpHandler(){
    m_handlers.insert({ID_USER_LOGIN,[](QJsonObject& jobj){}});
}
void LoginDialog::slot_login_http_finish(ReqId id,QString res,Ecode error){
    if(error!=Ecode::SUCCESS){
        Toast::error("网络错误");
    }
    else{
        QJsonDocument jdoc=QJsonDocument::fromJson(res.toUtf8());


        if(jdoc.isNull()||!jdoc.isObject()){
            // ShowTip("json解析错误",false);
            Toast::error("json解析错误",this);
            return;
        }
        QJsonObject json=jdoc.object();

        Toast::success("登录成功");
    }
}

bool LoginDialog::Validate(QString fieldId,QString value){
    if (fieldId == "username") {
        if (value.isEmpty()) {
            m_errorqueue.addError("username", "用户名不能为空");
            ui->user_edit->setStyleSheet("border: 2px solid red;");
            return false;
        }
        return true;
    }
    else if (fieldId == "password") {
        if (value.length() < 6) {
            m_errorqueue.addError("password", "密码至少需要6个字符");
            ui->psw_edit->setStyleSheet("border: 2px solid red;");
            return false;
        }
        if (value.length() > 15) {
            m_errorqueue.addError("password", "密码不能超过15个字符");
            ui->psw_edit->setStyleSheet("border: 2px solid red;");
            return false;
        }
        return true;
    }
    return true;
}

void LoginDialog::onPswtextChange(){
    auto text=ui->psw_edit->text();
    if(Validate("password",text)){
        m_errorqueue.removeError("password");
        ui->psw_edit->setStyleSheet("border:2px solider green;");
    }
}
void LoginDialog::onUsertextChange(){

    auto text=ui->user_edit->text();

    if(Validate("username",text)){
        m_errorqueue.removeError("username");
        ui->user_edit->setStyleSheet("border:2px solider green;");
    }
}
LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::on_login_btn_clicked()
{
    bool Allvaild=true;
    if(!Validate("username",ui->user_edit->text())) Allvaild=false;
    if(!Validate("password",ui->psw_edit->text())) Allvaild=false;

    if(Allvaild){
        QJsonObject json;
        json["username"]=ui->user_edit->text();
        json["password"]=ui->psw_edit->text();
        _HttpMgr::GetInstance()->PostHttpReq(QUrl("http://127.0.0.1:8080/user_login"),json,ReqId::ID_USER_LOGIN,MoudleId::LOGIN);
    }
}

